package com.lansi.map.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lansi.company.bean.Company;
import com.lansi.company.service.ICompanyService;
import com.lansi.map.bean.Mapinfo;
import com.lansi.map.mapper.MapinfoMapper;
import com.lansi.map.service.sx.MapidAlreadyExistException;
import com.lansi.mapper.UserMapper;

@Service
public class MapinfoService implements IMapinfoService{

	@Resource//注入持久层接口对象
	private MapinfoMapper mapinfoMapper;
	@Resource
	private ICompanyService iCompanyService;
	@Resource
	private UserMapper userMapper;
	
	public void getInsertMapinfo(Mapinfo mapinfo) {
		String mapid = mapinfo.getMapid();
		//调用持久层的查询接口查询添加的信息是否存在
		Mapinfo mapinfo1 = mapinfoMapper.selectByMapid(mapid);
		if(mapinfo1 == null) {
			
			//调用持久层接口添加地图信息
			mapinfoMapper.insertMapinfo(mapinfo);
			
		}else {
			throw new MapidAlreadyExistException("地图名称已存在");
		}
		
		
	}

	public void delMapinfoByMapid(String mapid) {
		
		//调用持久层接口删除地图信息
		mapinfoMapper.deleteByMapid(mapid);
		
	}

	public void updateMapinfoByMapid(Mapinfo mapinfo) {
		
		//调用持久层接口根据地图名称修改地图信息
		mapinfoMapper.updateMapinfo( mapinfo);
		
	}

	public List<Mapinfo> getMapinfoBySn(String sn,Integer offset,Integer count) {
		//调用持久层接口根据公司编号sn查询信息
		return mapinfoMapper.selectBySn(sn,offset, count);
	}

	public Mapinfo getMapinfoByMapid(String mapid) {
		//调用持久层接口根据地图名称查询信息
		return mapinfoMapper.selectByMapid(mapid);
	}

	public Map<String, Object> getCompanySnName(String username,String prio) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		if("1".equals(prio)) {
			List<Company> list = iCompanyService.selectCompanySnName();
			map.put("list", list);
		}else {
			Company company = mapinfoMapper.selectByUsernameGetSnName(username);
			
			map.put("sn", company.getSn());
			map.put("name", company.getName());
		}
		return map;
	}

	//根据sn/mapid进行查询
		public Mapinfo getToMapinfoBySnMapid(String sn, String mapid) {
			Mapinfo mapinfo = mapinfoMapper.getMapinfoBySnMapid(sn, mapid);
			return mapinfo;
		}

		//获取所有地图信息和信息的条数
		public Map<String, Object> getMapinfo(Integer offset, Integer count) {
			
			List<Mapinfo> list = mapinfoMapper.getMapinfoAll(offset, count);
			Integer size = mapinfoMapper.getMapinfoAllNo();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", list);
			map.put("size", size);
			return map;
		}

		//根据sn获取地图信息
		public List<Mapinfo> getMapinfoByUsernameSn(String sn) {
			List<Mapinfo> list = mapinfoMapper.getMapinfoBySn(sn);
			return list;
		}
		
		//根据sn或mapid获取地图信息条数
		public Integer getSizeBySnName(String sn, String mapid) {
			Integer size = mapinfoMapper.getMapinfoNo(sn, mapid);
			return size;
		}

		//获取所有地图信息条数
		public Integer getSizeAll() {
			Integer size = mapinfoMapper.getMapinfoAllNo();
			return size;
		}


	
	
}
